package net.xo68.destiny.starter.worker;

import com.fasterxml.jackson.databind.ObjectMapper;
import net.xo68.destiny.contract.domain.report.DatagramSerializer;
import net.xo68.destiny.core.serialize.JobHandleDatagramSerializer;
import net.xo68.destiny.starter.worker.handler.JobLocalConfigStoreFactory;
import net.xo68.destiny.starter.worker.handler.impl.JobLocalConfigStoreFactoryImpl;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties({SchedulerWorkerProperties.class})
@ConditionalOnProperty(prefix = "destiny.worker", value = "enable",havingValue = "true", matchIfMissing = true)
@ComponentScan(basePackages={"net.xo68.destiny.starter.worker.sched",
        "net.xo68.destiny.starter.worker.config",
        "net.xo68.destiny.starter.worker.handler",
        "net.xo68.destiny.starter.worker.logger",
        "net.xo68.destiny.starter.worker.controller"})
public class SchedulerWorkerConfiguration {

    @Bean
    @ConditionalOnMissingBean(JobLocalConfigStoreFactory.class)
    public JobLocalConfigStoreFactory jobLocalConfigStoreFactory(SchedulerWorkerProperties schedulerWorkerProperties){
        return new JobLocalConfigStoreFactoryImpl(schedulerWorkerProperties);
    }

    @Bean
    @ConditionalOnMissingBean(DatagramSerializer.class)
    public DatagramSerializer datagramSerializer(ObjectMapper objectMapper){
        return new JobHandleDatagramSerializer(objectMapper);
    }
}
